package com.aiwei.tdjk.controller;

import com.aiwei.tdjk.common.Constants;
import com.aiwei.tdjk.dto.request.CustomsRefinvstdPagerRequest;
import com.aiwei.tdjk.entity.CustomsRefinvstd;
import com.aiwei.tdjk.entity.CustomsStandard;
import com.aiwei.tdjk.service.CustomsRefinvstdService;
import com.aiwei.tdjk.service.CustomsStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping(value = "/admin/customsRefinvstd")
public class CustomsRefinvstdController extends BaseController {

	@Autowired
	private CustomsRefinvstdService customsRefinvstdServce;

	@Autowired
    private CustomsStandardService customsStandardService;


	@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
	public String list(CustomsRefinvstdPagerRequest request, Model model) {
		model.addAttribute("page", customsRefinvstdServce.find(request));
		model.addAttribute("customsRefinvstd", request);
		return viewName("list");
	}

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {
        model.addAttribute(Constants.OP_NAME, "新增");
        if (!model.containsAttribute("m")) {
		CustomsRefinvstd m = new CustomsRefinvstd();
        model.addAttribute("m", m);
        }
        return viewName("editForm");
	}

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(
            Model model, @Valid @ModelAttribute("m") CustomsRefinvstd m, BindingResult result,
            RedirectAttributes redirectAttributes) {
        if (hasError(result)) {
        return showCreateForm(model);
        }
        //新增判断，海关规格是否存在
        CustomsStandard standard = customsStandardService.getByStandard(m.getCustomsStandard());
        if (standard==null){
            redirectAttributes.addFlashAttribute(Constants.ERROR, "海关规则不存在，新增失败");
            return redirectToUrl("list");
        }
        customsRefinvstdServce.add(m);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "新增成功");
        return redirectToUrl("list");
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute(Constants.OP_NAME, "修改");
        model.addAttribute("m", customsRefinvstdServce.get(id));
        return viewName("editForm");
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.POST)
	public String update(
        Model model, @Valid @ModelAttribute("m") CustomsRefinvstd m, BindingResult result,
        RedirectAttributes redirectAttributes) {
        if (hasError(result)) {
            return showUpdateForm(m.getId(), model);
        }
        //新增判断，海关规格是否存在
        if (m.getCustomsStandard()!=null){
            CustomsStandard standard = customsStandardService.getByStandard(m.getCustomsStandard());
            if (standard==null){
                redirectAttributes.addFlashAttribute(Constants.ERROR, "海关规则不存在，修改失败");
                return redirectToUrl("list");
            }
        }

        customsRefinvstdServce.update(m);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "修改成功");
        return redirectToUrl("list");
	}
}
